//登录
function login(parameter, password) {
    var url = base_url + "/user/login/";
    $.ajax({
        type: "post",    //请求方式
        async: false,    //是否异步
        url: url,
        data: {
            "parameter": parameter,
            "password": password
        },    //请求参数  token
        success: function (data) {
            if (data.code === 0) {
                localStorage.setItem("token", data.data);
                alert_to_user("登录成功");
                window.location.href = "../../index.html";
            } else {
                alert_to_user(data.msg);
                localStorage.removeItem("token");
            }
        },
        error: function () {
            alert_to_user("登录请求发送失败");
        }
    })
}

//退出登录
function quit() {
    localStorage.removeItem("token");
}

function if_username_exist(username) {
    var url = base_url + "/user/status/exist/";
    var status;
    $.ajax({
        type: "get",    //请求方式
        async: false,    //是否异步
        url: url,
        data: {
            "username": username
        },    //请求参数  token
        success: function (data) {
            if (data.code === 0) {
                status = 0;
            } else {
                status = 1;
            }
        },
        error: function () {
            status = -1; //请求失败
        }
    })
    return status;
}

function get_turing_code(parameter) {
    var url = base_url + "/user/verification/turing/";
    var base64str = null;
    $.ajax({
        type: "post",    //请求方式
        async: false,    //是否异步
        url: url,
        data: {
            "parameter": parameter
        },    //请求参数  token
        success: function (data) {
            if (data.code === 0) {
                base64str = data.data;
            } else {
                alert_to_user("请求失败，" + data.msg);
            }
        },
        error: function () {
            alert_to_user("请求失败");
        }
    })
    return base64str;
}

function get_verification_code(parameter, verificationCode) {
    var url = base_url + "/user/verification/message/";
    var status = -1;
    $.ajax({
        type: "post",    //请求方式
        async: false,    //是否异步
        url: url,
        data: {
            "parameter": parameter,
            "verificationCode": verificationCode
        },    //请求参数  token
        success: function (data) {
            if (data.code === 0) {
                status = 1;
                alert_to_user("发送成功");
            } else {
                status = 0;
                alert_to_user("请求失败，" + data.msg);
            }
        },
        error: function () {
            alert_to_user("请求失败");
        }
    })
    return status;
}

function do_register(username, parameter, verificationCode, password) {
    var url = base_url + "/user/register/";
    var status = 0;
    $.ajax({
        type: "post",    //请求方式
        async: false,    //是否异步
        url: url,
        data: {
            "username": username,
            "parameter": parameter,
            "verificationCode": verificationCode,
            "password": password
        },    //请求参数  token
        success: function (data) {
            if (data.code === 0) {
                status = 1;
                alert_to_user("注册成功");
            } else {
                alert_to_user("注册失败，" + data.msg);
            }
        },
        error: function () {
            alert_to_user("请求失败");
        }
    })
    return status;
}

function do_reset(parameter, verificationCode, password) {
    var url = base_url + "/user/register/reset/";
    var status = 0;
    $.ajax({
        type: "post",    //请求方式
        async: false,    //是否异步
        url: url,
        data: {
            "parameter": parameter,
            "verificationCode": verificationCode,
            "password": password
        },    //请求参数  token
        success: function (data) {
            if (data.code === 0) {
                status = 1;
                alert_to_user("密码重置成功");
            } else {
                alert_to_user("密码重置失败，" + data.msg);
            }
        },
        error: function () {
            alert_to_user("请求失败");
        }
    })
    return status;
}
